﻿using Rushabh.Xna.Framework;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Windows.Foundation;
using Windows.Foundation.Collections;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Controls.Primitives;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Input;
using Windows.UI.Xaml.Media;
using Windows.UI.Xaml.Navigation;

// The Blank Page item template is documented at http://go.microsoft.com/fwlink/?LinkId=234238

namespace Game
{
    /// <summary>
    /// An empty page that can be used on its own or navigated to within a Frame.
    /// </summary>
    public sealed partial class MainPage : Page, IGame
    {
        Texture2D _texture;
        SpriteBatch spriteBatch;
        GameTime timer = new GameTime();
        Rectangle _rect = new Rectangle(0, 100, 100, 100);
        Texture2D _texutre2;
        public MainPage()
        {
            this.InitializeComponent();

            GraphicsDevice.setCanvas(GameCanvas);
            timer.Interval = TimeSpan
                .FromTicks(66666);
            timer.Tick += (obj, args) =>
            {
                Update();
                Draw();
                timer.GameTimeUpdate();
                timer.GameTimeUpdate(66666);
            };

            Initialize();
            LoadComponent();

            Window.Current.SizeChanged += Current_SizeChanged;
        }

        void Current_SizeChanged(object sender, Windows.UI.Core.WindowSizeChangedEventArgs e)
        {
            //Handle your snap view here
        }

        public async void Initialize()
        {
            spriteBatch = new SpriteBatch(GameCanvas, Dispatcher);
            _texture = await Texture2D.LoadComponent(new Uri("ms-appx:///icon.png", UriKind.RelativeOrAbsolute), "1");
            _texutre2 = await Texture2D.LoadComponent(new Uri("ms-appx:///2.jpg", UriKind.RelativeOrAbsolute), "2");
        }

        public void LoadComponent()
        {
            timer.Start();
        }

        public async void Draw()
        {
            GraphicsDevice.Clear(Colors.Red);
            spriteBatch.Draw(_texture, _rect, Colors.Wheat);
            spriteBatch.Draw(_texutre2, new Rectangle() { x = _rect.y, y = _rect.x }, Colors.Wheat);
        }

        public void Update()
        {
            if (_rect.x < 1366)
                _rect.x += 10;
            else
                _rect.x = 0;
        }

        private void GameCanvas_Tapped_1(object sender, TappedRoutedEventArgs e)
        {
            Point p = e.GetPosition(sender as UIElement);
            if (_rect.Contains(p))
                _rect.y-= 10;

            p = e.GetPosition(sender as Image);
            if (_rect.Contains(p))
                _rect.y -= 10;
        }

        
    }
}
